import DoctorEvaluationInfo from '@/components/EvaluationRecord/components/DoctorEvaluationInfo';
import { get_assess_list } from '@/services/assrssment';
import age from '@/utils/age';
import { PageContainer, ProColumns, ProTable } from '@ant-design/pro-components';
import { useState } from 'react';

type GithubIssueItem = {
  id: number;
  phone: string;
  username: string;
  sex: number;
  realname: string;
  birth: string;
  platform_username: string;
  department_name: string;
  create_time: string;
  days_number: number;
  end_date: string;
};
const Assessment_record = () => {
  // 评估详情模态框状态
  const [doctorisModalOpen, setDoctorIsModalOpen] = useState(false);
  // 存储当前评估记录id
  const [doctorId, setDoctorId] = useState<number>();

  // 控制评估详情模态框的显示与隐藏
  const isShowDoctor = (status: boolean) => {
    setDoctorIsModalOpen(status);
  };

  // 获取评估记录列表
  const get_assessment_list = async (params: {
    pageSize?: number | undefined;
    current?: number | undefined;
    keyword?: string | undefined;
  }) => {
    let request_body = {
      ...params,
      current: params.current,
      pageSize: params.pageSize,
    };
    let res: any = await get_assess_list(request_body);
    return {
      data: res.data.list,
      success: true,
      total: res.data.total_count,
    };
  };
  /* 评估记录表格列 */
  const columns: ProColumns<GithubIssueItem>[] = [
    {
      title: '序号',
      dataIndex: 'index',
      hideInSearch: true,
      valueType: 'index',
      align: 'center',
    },
    {
      title: '手机号',
      dataIndex: 'phone',
      align: 'center',
    },
    {
      title: '患者姓名',
      dataIndex: 'username',
      align: 'center',
    },
    {
      title: '性别',
      dataIndex: 'sex',
      align: 'center',
      valueEnum: {
        0: {
          text: '未知',
        },
        1: {
          text: '男',
        },
        2: {
          text: '女',
        },
      },
    },
    {
      title: '年龄',
      dataIndex: 'birth',
      hideInSearch: true,
      align: 'center',
      render: (_, record) => {
        return age(record.birth);
      },
    },
    {
      title: '科室',
      dataIndex: 'department_name',
      hideInSearch: true,
      align: 'center',
    },
    {
      title: '评估医生',
      dataIndex: 'platform_username',
      hideInSearch: true,
      align: 'center',
    },
    {
      title: '评估时间',
      dataIndex: 'create_time',
      width: 200,
      hideInSearch: true,
      align: 'center',
    },
    {
      title: '评估时间',
      dataIndex: 'create_time',
      valueType: 'dateRange',
      hideInTable: true,
      colSize: 2,
      search: {
        transform: (value) => {
          return {
            start_date: value[0],
            end_date: value[1],
          };
        },
      },
    },
    {
      title: '训练周期',
      dataIndex: 'days_number',
      hideInSearch: true,
      align: 'center',
    },
    {
      title: '训练结束时间',
      dataIndex: 'end_date',
      hideInSearch: true,
      align: 'center',
    },
    {
      title: '操作',
      hideInSearch: true,
      key: 'action',
      align: 'center',
      render: (_, record) => [
        <a
          key="check"
          onClick={() => {
            setDoctorId(record.id);
            isShowDoctor(true);
          }}
        >
          详情
        </a>,
      ],
    },
  ];
  return (
    <PageContainer breadcrumb={false}>
      <ProTable<GithubIssueItem>
        columns={columns}
        cardBordered
        request={async (params = {}, sort, filter) => get_assessment_list(params)}
        editable={{
          type: 'multiple',
        }}
        rowKey={(record) => record.id}
        search={{
          labelWidth: 'auto',
          collapseRender: false,
          collapsed: false,
          span: 4,
        }}
        options={{
          setting: {
            listsHeight: 400,
          },
        }}
        pagination={{
          pageSize: 10,
        }}
        dateFormatter="string"
        headerTitle="评估记录列表"
      />
      {/* 评估记录详情 */}
      {doctorisModalOpen ? (
        <DoctorEvaluationInfo
          doctorId={doctorId}
          doctorisModalOpen={doctorisModalOpen}
          isShowDoctor={isShowDoctor}
        ></DoctorEvaluationInfo>
      ) : (
        ''
      )}
    </PageContainer>
  );
};
export default Assessment_record;
